package lab4TwiceTest;

import lab2.Employee;

import java.lang.reflect.InvocationTargetException;
import java.util.Random;

public class HRVisitor implements EmployeeVisitor{

    int bestHours = Integer.MIN_VALUE;
    String bestName;
    int worstHours = Integer.MAX_VALUE;
    String worstName;

    static int PresetTime = 40;//每周工作40小时
    Random r = new Random();
    @Override
    public void visit(Employee e) {
        String name = "";
        try {
            name = (String) e.getClass().getDeclaredMethod("getName",null).invoke(e);
        } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) {
            throw new RuntimeException(ex);
        }
        switch (e.getClass().getSimpleName()) {
            case "RegularStaff":
                regularStaffVisit(e, name);
                break;
            case "Intern":
                internVisit(e, name);
                break;
            //...
            default:
                throw new IllegalArgumentException("Unsupported employee type: " + e.getClass().getSimpleName());
        }

    }

    private void internVisit(Employee e, String name) {
        int hours = r.nextInt(50);
        try {
            e.getClass().getDeclaredMethod("setWorkingHours",int.class).invoke(e,hours);
            System.out.println("Intern:"+name+"'s working time is"+hours);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
        if (hours > bestHours){
            bestHours = hours;
            bestName = name;
        }
        if (hours < worstHours){
            worstHours = hours;
            worstName = name;
        }
    }

    private void regularStaffVisit(Employee e, String name) {
        //随机生成员工的加班时长和请假时长
        int overtime = r.nextInt(30);
        int leaveTime = r.nextInt(40);
        //计算员工的总工时
        int workingHours = PresetTime + overtime + leaveTime;
        try {
            e.getClass().getDeclaredMethod("setOvertime",int.class).invoke(e,overtime);
            e.getClass().getDeclaredMethod("setLeaveTime",int.class).invoke(e,leaveTime);
            e.getClass().getDeclaredMethod("setWorkingHours",int.class).invoke(e,workingHours);
            System.out.println("RegularStaff:"+name+"'s working time is"+workingHours);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) {
            throw new RuntimeException(ex);
        }
        if (workingHours > bestHours){
            bestHours = workingHours;
            bestName = name;
        }
        if (workingHours < worstHours){
            worstHours = workingHours;
            worstName = name;
        }
    }

    public String getBestEmployee(){
        System.out.println("The best employee is"+bestName);
        return bestName;
    }

    public String getWorstName(){
        System.out.println("The worst employee is"+worstName);
        return worstName;
    }
}
